package th;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/24 11:47
 * @description 线程死锁(多个线索抢用一个资源)
 */
public class ThreadDeadLock {

    public static void main(String[] args) {
        MakeUp me = new MakeUp(0, "me");
        MakeUp you = new MakeUp(1, "you");
        me.start();
        you.start();
    }
}

class LisStick {

}

class Mirror {

}

class MakeUp extends Thread {

    static LisStick lisStick = new LisStick();
    static Mirror mirror = new Mirror();
    int choice;
    String name;

    public MakeUp(int choice, String name) {
        this.choice = choice;
        this.name = name;
    }


    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lisStick) {
                System.out.println("get lisStick");
                Thread.sleep(1000);
            }
            synchronized (mirror) {
                System.out.println("one second get mirror");
            }
        } else {
            synchronized (mirror) {
                System.out.println("one second get mirror");
                Thread.sleep(2000);
            }
            synchronized (lisStick) {
                System.out.println("get lisStick");
            }
        }
    }
}
